package com.example.test.service.Impl;

import com.example.test.entity.Order;
import com.example.test.entity.Product;
import com.example.test.mapper.OrderMapper;
import com.example.test.mapper.ProductMapper;
import com.example.test.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Order> getAllOrders() {
        List<Order> orders = orderMapper.findAllOrders();
        for (Order order : orders) {
            try {
                List<Product> products = orderMapper.findProductsByOrderId(order.getId());
                for (Product product : products) {
                    String review = orderMapper.findReviewByOrderProductAndUsername(order.getId(), product.getId(), order.getUsername());
                    if (review != null) {
                        product.setReview(review);
                    }
                }
                order.setProducts(products);
            } catch (Exception e) {
                // 记录错误日志
                System.err.println("Error fetching products for order id: " + order.getId());
                e.printStackTrace();
            }
        }
        return orders;
    }

    @Override
    public Order getOrderById(Long id) {
        Order order = orderMapper.findOrderById(id);
        if (order != null) {
            List<Product> products = orderMapper.findProductsByOrderId(id);
            for (Product product : products) {
                String review = orderMapper.findReviewByOrderProductAndUsername(id, product.getId(), order.getUsername());
                if (review != null) {
                    product.setReview(review);
                }
            }
            order.setProducts(products);
        }
        return order;
    }

    @Override
    public Order createOrder(Order order) {
        orderMapper.insertOrder(order);
        Long orderId = order.getId();

        for (Product product : order.getProducts()) {
            orderMapper.insertOrderProduct(orderId, product.getId(), product.getCount());
        }
        return order;
    }

    @Override
    public void cancelOrder(Long orderId, String reason) {
        orderMapper.updateOrderStatus(orderId, "取消");
        orderMapper.insertRefundReason(orderId, reason);
    }

    @Override
    public void requestRefund(Long orderId, String reason) {
        orderMapper.updateOrderStatus(orderId, "申请退款中");
        orderMapper.insertRefundReason(orderId, reason);
    }

    @Override
    public void submitReview(Long orderId, Long productId, String review,String username) {
        if (orderMapper.reviewExists(orderId, productId, username) > 0) {
            orderMapper.updateReview(orderId, productId, review, username);
        } else {
            orderMapper.insertReview(orderId, productId, review, username);
        }
    }

    @Override
    public void updateOrderStatus(Long orderId, String status) {
        orderMapper.updateOrderStatus(orderId, status);
    }

    @Override
    public List<Order> getRefundRequests() {
        return orderMapper.findOrdersByStatus("申请退款中");
    }

    @Override
    public List<Map<String, Object>> getPendingReviews() {
        return orderMapper.findPendingReviews();
    }

    @Override
    public void updateReviewStatus(Long reviewId, boolean iflegal) {
        orderMapper.updateReviewStatus(reviewId, iflegal);
    }
    @Override
    public void updateProductInventory(Long orderId, String status) {
        if ("退款成功".equals(status)) {
            List<Product> products = orderMapper.findProductsByOrderId(orderId);
            for (Product product : products) {
                int refundedQuantity = product.getOrderQuantity();
                productMapper.updateProductQuantity(product.getId(), refundedQuantity);
            }
        }
    }
}